﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Component;
using System.IO;

public partial class BackEnd_GalleryList : System.Web.UI.UserControl
{
    protected string onclick = "";
    public string nameImage = "";
    public string _Url = BackEndQuery._AdminQuery + ConstString.NewL;
    FileInfo[]alFile=null;
    protected void Page_Load(object sender, EventArgs e)
    {
        DataBind(GalleryDB.GetAll());
        this.onclick = @"OpenWindowUploadFile(""FileManagement/File.aspx?"+BackEndQuery._GalleryL+@"=true"",""window"",""width = 630px, height =520px"");";
        grid.RowCancelingEdit += delegate {  Response.Redirect(Request.Url.ToString()); };
    }
    /// <summary>
    ///   public void ReGetAllImge(object sender, System.EventArgs e)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void ReGetAllImge(object sender, System.EventArgs e)
    {
        GetAllImg();      
        DataBind(GalleryDB.GetAll());
    }
    /// <summary>
    ///    public void GetAllImg()
    /// </summary>
    public void GetAllImg()
    {
        string path = Server.MapPath("FileUpload//Gallery");
        alFile = GalleryDB.GetAllFileImg(path);          
        foreach (FileInfo file in alFile)
        {
            string path1= "FileUpload/Gallery/" + file.Name;
            if (GalleryDB.TestExistFile(path1)==false)
            {Gallery info = new Gallery(KetNoiCSDL.session);
            info.Path =path1;
            info.Priority = 0;
            
            GalleryDB.Save(info);
            }           
        }
    }
   
  /// <summary>
    ///     void DataBind(object al)
  /// </summary>
  /// <param name="al"></param>
    void DataBind(object al)
    {
        if (al != null)
        {
            grid.DataKeyNames = new string[] { ColumGallery._Id };
            grid.DataSource = al;
            grid.DataBind();
        }
    }
  
    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;       
        TextBox txtPriority = (TextBox)grid.Rows[e.RowIndex].FindControl("txtPriority");
        Gallery intro = new Gallery(KetNoiCSDL.session);
        intro = GalleryDB.GetObjectByid(ConvertType.ToInt(id),true);
        intro.Priority = ConvertType.ToInt(txtPriority.Text.Trim());
        if (GalleryDB.Save(intro) == 1)
        {
            this.lbMessage.Text = ConstString.capNhatTC;
        }
         Response.Redirect(Request.Url.ToString());
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            object id = grid.DataKeys[e.RowIndex].Value;
            Gallery  intro = GalleryDB.GetObjectByid(ConvertType.ToInt(id),true);
            if (GalleryDB.Delete(intro) == 1)
            {
                string path1 = HttpRuntime.AppDomainAppPath + intro.Path.Replace("../","").Replace("/","\\");
                FileInfo fi = new FileInfo(path1);
                if(fi.Exists==true)
                    fi.Delete();
                this.lbMessage.Text = ConstString.xoaTC;
            }
        }
        catch { }
        DataBind(Component.GalleryDB.GetAll());
    }

    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        DataBind(Component.GalleryDB.GetAll());
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        DataBind(Component.GalleryDB.GetAll());
    }
  
}
